<template>
  <div class='container'>
    <el-dialog
      :title="dialogForm.id ? '修改学科' : '新增学科'"
      :visible="dialogVisible"
      width="400px"
      @close='close'>
      <el-form :model="dialogForm" ref="dialogForm" :rules="dialogFormRules">
        <el-form-item label="学科名称" label-width="80px" prop="subjectName">
          <el-input v-model="dialogForm.subjectName" placeholder="请输入学科名称"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isFrontDisplay" label-width="80px">
          <el-switch
            v-model="dialogForm.isFrontDisplay"
            :active-value='1'
            :inactive-value='0'
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { update, add } from '@/api/hmmm/subjects'
export default {
  name: 'SubjectsAdd',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      dialogFormRules: {
        subjectName: [
          { required: true, message: '请输入学科名称' }
        ]
      },
      dialogForm: {
        subjectName: null,
        isFrontDisplay: 1
      }
    }
  },
  methods: {
    close () {
      this.$emit('update:dialogVisible', false)
      this.$refs.dialogForm.resetFields()
      this.dialogForm = {
        subjectName: null,
        isFrontDisplay: 1
      }
    },
    async add () {
      try {
        await this.$refs.dialogForm.validate()
      } catch {
        return false
      }
      if (this.dialogForm.id) {
        await update(this.dialogForm)
        this.$message.success('修改学科成功')
      } else {
        await add(this.dialogForm)
        this.$emit('page', 1)
        this.$message.success('新增学科成功')
      }
      this.$emit('getSubjectsList')
      this.close()
    }
  }
}
</script>

<style scoped lang='scss'></style>
